﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Type;
using NHibernate.Expression;

namespace TuMama
{
    public class NHibernateManager
    {
        private Configuration configuration;

        private ISessionFactory factory;

        private ISession session;

        public ISession Session
        {
            get
            {
                if (session == null || !session.IsOpen)
                {
                    try
                    {
                        session = factory.OpenSession();
                    }
                    catch (Exception e)
                    {
                        throw new System.ApplicationException("La sesion con la Base de datos no pudo ser creada", e);
                    }
                }
                return session;
            }
        }

        public NHibernateManager()
        {
            try
            {
                configuration = new Configuration();
                configuration.AddAssembly("TuMama");
                //configuration.AddClass(typeof(LogEntry));
                factory = configuration.BuildSessionFactory();
            }
            catch (Exception ex)
            {
                throw new System.ApplicationException("No se pudo crear la configuración NHibernate", ex);
            }
        }

        public void closeSession()
        {
            session.Close();
        }
    }
}
